var e = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
        var n, o = arguments[t];
        for (n in o) Object.prototype.hasOwnProperty.call(o, n) && (e[n] = o[n]);
    }
    return e;
}, t = require("../api/user"), n = u(require("../store/index")), o = u(require("../libs/x/index")), r = u(require("./qs/index")), i = u(require("./storage"));

function u(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var a = require("../siteinfo"), f = function() {
    return new Promise(function(e, n) {
        o.default.login().then(function(o) {
            var r = o.code;
            o = {
                "设备型号": (o = wx.getSystemInfoSync()).model,
                "微信版本号": o.version,
                "操作系统及版本": o.system,
                "客户端平台": o.platform,
                "客户端基础库版本": o.SDKVersion,
                "定位开关": o.locationAuthorized ? "已打开" : "已关闭",
                "WiFi开关": o.wifiEnabled ? "已打开" : "已关闭"
            };
            (0, t.loginWithCode)(r, o).then(function(t) {
                i.default.setItem("session_key", t.session_key), i.default.setItem("openid", t.openid), 
                e(t.user);
            }).catch(n);
        }).catch(n);
    });
}, s = [ "", " ", "  ", "   ", "    ", "     ", "      ", "       ", "        ", "         " ], c = function(e, t, n) {
    if ((t -= (e += "").length) <= 0) return e;
    if (n || 0 === n || (n = " "), " " == (n += "") && t < 10) return s[t] + e;
    for (var o = ""; (t >>= 1) && (1 & t && (o += n), t >>= 1); ) n += n;
    return o + e;
}, l = r.default.parse, d = {
    wxLogin: function() {
        return new Promise(function(e, t) {
            f().then(function(t) {
                n.default.data.userInfo = t, n.default.getCurrentUserCallBack && "function" == typeof n.default.getCurrentUserCallBack && n.default.getCurrentUserCallBack(t), 
                n.default.getUserInfo && "function" == typeof n.default.getUserInfo && n.default.getUserInfo(t), 
                e(t);
            }).catch(function(e) {
                i.default.clear("openid"), t(e);
            });
        });
    },
    fillPath: function(e) {
        var t = /https?:\/\//.test(e);
        return /^\/\//.test(e) || t ? e : /https?:\/\/[a-z0-9.]+\//.exec(a.siteroot)[0] + e;
    },
    leftPad: c,
    leftpad: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 2, n = (n = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : "0") || "0";
        if (e) {
            for (e = e.toString(); e.length < t; ) e = n + e;
            return e;
        }
        return "00";
    },
    convertSecond: function(e) {
        if (e) {
            var t = c(Math.round(e % 60), 2, "0");
            return c(Math.floor(e / 60), 2, "0") + ":" + t;
        }
        return "00:00";
    },
    queryString: r.default,
    parse: l,
    getUserMobile: function(e, r) {
        return new Promise(function(u, a) {
            o.default.checkSession().then(function() {
                var o = i.default.getItem("session_key"), a = i.default.getItem("openid");
                (0, t.decodeUserMobile)({
                    openid: a,
                    session_key: o,
                    iv: r,
                    encrypted: e
                }).then(function(e) {
                    e = e.mobile, n.default.data.userInfo.mobile = e, u(e);
                });
            }).catch(function(o) {
                a(o), f().then(function(o) {
                    var i = getItem("openid");
                    (0, t.decodeUserMobile)({
                        openid: i,
                        session_key: o,
                        iv: r,
                        encrypted: e
                    }).then(function(e) {
                        e = e.mobile, n.default.data.userInfo.mobile = e, u(e);
                    }).catch(a);
                });
            });
        });
    },
    toastText: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "none", n = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : 2e3;
        return new Promise(function(o, r) {
            wx.showToast({
                title: e,
                icon: t,
                duration: n,
                success: o,
                fail: r
            });
        });
    },
    modalText: function(t) {
        var n = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "我知道了", o = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : {};
        return new Promise(function(r, i) {
            "string" != typeof t && (t = JSON.stringify(t)), wx.showModal(e({
                content: t,
                showCancel: !1,
                confirmText: n
            }, o, {
                success: function(e) {
                    r(e);
                },
                fail: function(e) {
                    i(e);
                }
            }));
        });
    },
    alwaysBack: function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "reLaunch", t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "/pages/home/index";
        return new Promise(function(n, o) {
            wx.navigateBack({
                delta: 1,
                success: n,
                fail: function(n) {
                    wx[e]({
                        url: t
                    }), o(n);
                }
            });
        });
    },
    after: function(e) {
        e && "function" == typeof e && setTimeout(function() {
            e();
        }, 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 800);
    },
    getCurrentDate: function() {
        var e = Date.parse(new Date());
        return (e = new Date(e)).getFullYear() + "/" + (e.getMonth() + 1 < 10 ? "0" + (e.getMonth() + 1) : e.getMonth() + 1) + "/" + (e.getDate() < 10 ? "0" + e.getDate() : e.getDate());
    }
};

module.exports = d;